//
//  LookResume.m
//  HappyStore
//
//  Created by 李小辉 on 16/7/6.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LookResume.h"
#import "skillLabelCell.h"
#import "workExpCell.h"
#import "educationCell.h"

@interface LookResume ()

@end

@implementation LookResume

- (NSDictionary *)dicData{
    if (!_dicData) {
        _dicData = [NSDictionary dictionary];
    }
    return _dicData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.dicData[@"account"] == nil) {
        [self startNetWork];
    }
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"简历预览";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self tableHeaderView];
    //设置投递简历按钮
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, 100)];
    footerView.backgroundColor = [UIColor whiteColor];
    
    if ([self.resumeBtnTig isEqualToString:@"resumeBtnTig"]) {

    }else{
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(22, 50, screenW-22-22, 40)];
        button.backgroundColor = RGB(64, 177, 241);
        button.layer.cornerRadius = 4.0f;
        button.layer.masksToBounds = YES;
        [button setTitle:@"投递简历" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickResumeBtn) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:button];
        self.tableView.tableFooterView = footerView;
    }
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
-(void)tableHeaderView{
    //设置头视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, 140)];
    view.backgroundColor = RGB(64, 177, 241);
    //头像
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(50, 20, 75, 75)];
    imageview.layer.cornerRadius = imageview.frame.size.width/2;
    imageview.layer.masksToBounds = YES;
    [imageview sd_setImageWithURL:[NSURL URLWithString:self.dicData[@"headPhoto"]]];
    //姓名/地址，性别
    UILabel *nameLabel = [[UILabel alloc] init];
    if ([self.dicData[@"userName"] isEqualToString:@""] || [self.dicData[@"userName"] isEqualToString:@"null"]) {
        //address , sex;
        nameLabel.text = self.dicData[@"account"];
    }else{
        nameLabel.text = self.dicData[@"userName"];
        if (self.dicData[@"sex"] != nil || ![self.dicData[@"sex"] isEqualToString:@"null"]) {
            nameLabel.text = [nameLabel.text stringByAppendingString:[NSString stringWithFormat:@"(%@)",self.dicData[@"sex"]]];
        }
    }
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = [UIFont systemFontOfSize:12];
    CGSize namelabelSize = [nameLabel sizeThatFits:CGSizeMake(MAXFLOAT, 20)];
    nameLabel.frame = CGRectMake(imageview.x + 12 + 75, imageview.y, namelabelSize.width, namelabelSize.height);
    //职位
    UILabel *positionLabel = [[UILabel alloc] init];
    positionLabel.textColor = [UIColor whiteColor];
    positionLabel.backgroundColor = [UIColor clearColor];
    positionLabel.font = [UIFont systemFontOfSize:12];
    positionLabel.text = [NSString stringWithFormat:@"职位：%@", self.dicData[@"positionName"]];
    CGSize positionSize = [positionLabel sizeThatFits:CGSizeMake(MAXFLOAT, 20)];
    positionLabel.frame = CGRectMake(nameLabel.x, nameLabel.y + namelabelSize.height +10 , positionSize.width, positionSize.height);
    //手机
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(nameLabel.x, positionLabel.y + positionSize.height + 10, screenW - 150, positionSize.height)];
    phoneLabel.textColor = [UIColor whiteColor];
    phoneLabel.backgroundColor = [UIColor clearColor];
    phoneLabel.font = [UIFont systemFontOfSize:12];
    phoneLabel.text = [NSString stringWithFormat:@"手机：%@" ,self.dicData[@"phone"]];
    //邮箱
    UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(nameLabel.x, phoneLabel.y + positionSize.height+ 10, screenW - 150, positionSize.height)];
    emailLabel.textColor = [UIColor whiteColor];
    emailLabel.backgroundColor = [UIColor clearColor];
    emailLabel.font = [UIFont systemFontOfSize:12];
    emailLabel.text = [NSString stringWithFormat:@"邮箱：%@" ,self.dicData[@"email"]];
    
    [view addSubview:imageview];
    [view addSubview:nameLabel];
    [view addSubview:positionLabel];
    [view addSubview:phoneLabel];
    [view addSubview:emailLabel];
    
    self.tableView.tableHeaderView = view;
}

- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return [self.dicData[@"work"] count];
    }else
        return [self.dicData[@"education"] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {//求职信息
        skillLabelCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"skillLabelCell" owner:self options:nil] lastObject];
        cell.moneyLabel.text = self.dicData[@"paymentRange"];
        cell.industry.text = self.dicData[@"industry"];
        cell.workExp.text = self.dicData[@"experience"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 1){//技能标签
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        NSArray *arrayLabel = [self.dicData[@"skillLable"] componentsSeparatedByString:@","];
        for (int i = 0; i<arrayLabel.count; i++) {
            UILabel *labelLabel = [[UILabel alloc] init];
            labelLabel.text = arrayLabel[i];
            
            //文字居中
            labelLabel.textAlignment = NSTextAlignmentCenter;
            labelLabel.font = [UIFont systemFontOfSize:12];
            labelLabel.textColor = RGB(23, 176, 216);
            labelLabel.backgroundColor = RGB(217, 242, 255);
            //设置边框
            labelLabel.layer.borderWidth = 0.5f;
            labelLabel.layer.borderColor = RGB(23, 176, 216).CGColor;
            //自定义宽度
            labelLabel.numberOfLines = 1;
            labelLabel.lineBreakMode = NSLineBreakByCharWrapping;
            CGSize maxSize = CGSizeMake(MAXFLOAT, 20);
            
            CGSize size = [labelLabel sizeThatFits:maxSize];
        
            labelLabel.frame = CGRectMake(i%3*100 +20, i/3 * 20 + 4, 70,size.height + 3);
            
            if (labelLabel.text.length!=0) {
                [cell addSubview:labelLabel];
            }
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 2){//工作经历
        workExpCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"workExpCell" owner:self options:nil] lastObject];
        NSDictionary *workDic = self.dicData[@"work"][indexPath.row];
        cell.workCompanyName.text = workDic[@"workName"];
        cell.workTime.text = workDic[@"workTime"];
        cell.positionLabel.text = workDic[@"workPosition"];
        cell.workMiaosu.text = workDic[@"workMiaosu"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//不可选择状态
        return cell;
    }else{//教育经历
        educationCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"educationCell" owner:self options:nil] lastObject];
        NSDictionary *schoolDic = self.dicData[@"education"][indexPath.row];
        cell.schoolName.text = schoolDic[@"schoolName"];
        cell.schoolTime.text = schoolDic[@"schoolTime"];
        cell.schoolStudy.text = schoolDic[@"schoolStudy"];
        cell.schoolMiaos.text = schoolDic[@"schoolMiaosu"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 86;
    }else if (indexPath.section == 1){
        return 44;
    }else if (indexPath.section == 2){
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:12];
        label.numberOfLines = 0;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.text = self.dicData[@"work"][indexPath.row][@"workMiaosu"];
        CGSize labelSize = [label sizeThatFits:CGSizeMake(screenW-16, MAXFLOAT)];
        return 104 + labelSize.height - 15;
    }else{
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:12];
        label.numberOfLines = 0;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.text = self.dicData[@"education"][indexPath.row][@"schoolMiaosu"];
        CGSize labelSize = [label sizeThatFits:CGSizeMake(screenW-16, MAXFLOAT)];
        return 100 + labelSize.height - 15;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = RGB(247, 247, 247);
    NSArray *imgAry = @[@"求职信息",@"技能标签",@"工作经历",@"教育经历"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgAry[section]]];
    CGRect frame = imageview.frame;
    frame.origin.x = 8;
    frame.origin.y = 8;
    imageview.frame = frame;
    [view addSubview:imageview];
    
    return view;
}
#pragma mark - 请求数据
- (void)startNetWork{
    [self showRoundProgressWithTitle:@"请稍后..."];
    NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
    muDicParameters[@"userType"] = [USER_DEFAULTS objectForKey:@"usertype"];
    muDicParameters[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
    muDicParameters[@"token"] = [USER_DEFAULTS objectForKey:@"token"];
    
    [LSHttpTool sendHttpData:@"MyBasicMessage_app_myHome.im.lensum" params:muDicParameters success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([dicInfo[@"result"] integerValue] == 1) {
            self.dicData = dicInfo[@"data"];
            [self tableHeaderView];
            [self.tableView reloadData];

            [self hideBubble];
        }else

        [self hideBubble];
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:error.localizedDescription];

    }];
}
#pragma mark - 投递简历
- (void)clickResumeBtn{
        NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
        //token=验证用户&type=类型（1 投递 2邀请）&jobId=招聘求职ID&sendUserId=登录用户的id&receiveUserId=发送招聘求职的id
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"sendUserId"];
        [muDicParameters setObject:self.jobId forKey:@"jobId"];
        [muDicParameters setObject:self.userid forKey:@"receiveUserId"];//发送招聘求职的id
        [muDicParameters setObject:@(1) forKey:@"type"];//类型（1 投递 2邀请）
    
        NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"delivery_invitation_app_jobhunting.im.lensum"];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
            //NSLog(@"%@",dicInfo);
            if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {

                [self showRightWithTitle:dicInfo[@"msg"] autoCloseTime:2];

               //延迟1s执行
                [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];
               
            }else {

                [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
            }
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            [self showSingleIconBubble:error.localizedDescription];

        }];
    
}

- (void)delayMethod{
    UIViewController * viewVC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 3];
    [self.navigationController popToViewController:viewVC animated:YES];
}
@end
